
package com.kaciula.letsdoit.ui;

import android.content.Context;
import android.content.SharedPreferences;

public class PreferenceHelper {

    private Context mCtx;

    private SharedPreferences mGeneral;
    
    private SharedPreferences mCredentials;

    public PreferenceHelper(Context ctx) {
        this.mCtx = ctx;
        mGeneral = this.mCtx.getSharedPreferences(Constants.PREFS_GENERAL, Context.MODE_PRIVATE);
        mCredentials = ctx.getSharedPreferences(Constants.PREFS_CREDENTIALS,
                Context.MODE_PRIVATE);
    }

    public String getPicName(long id, int no) {
        String name = mGeneral.getString(Constants.PREFS_KEY_NAME, null);
        String surname = mGeneral.getString(Constants.PREFS_KEY_SURNAME, null);
        
        return String.format(Constants.PIC_NAME_FORMAT, surname, name, id, no);
    }
    
    public String getPicPath(long id, int no) {
        String picsPath = mGeneral.getString(Constants.PREFS_KEY_PICS_PATH, null);
        String picName = getPicName(id, no);
        
        return String.format("%s/%s", picsPath, picName);
    }
    
    public void saveCredentials(String username, String pass, String md5Pass) {
        SharedPreferences.Editor editor = mCredentials.edit();
        editor.putBoolean(Constants.PREFS_KEY_IS_VALID, true);
        editor.putString(Constants.PREFS_KEY_USERNAME, username);
        editor.putString(Constants.PREFS_KEY_PASSWORD, pass);
        editor.putString(Constants.PREFS_KEY_MD5_PASSWORD, md5Pass);
        editor.commit();
    }
    
    public String getUsername() {
        return mCredentials.getString(Constants.PREFS_KEY_USERNAME, "");
    }
    
    public String getMd5Pass() {
        return mCredentials.getString(Constants.PREFS_KEY_MD5_PASSWORD, "");
    }
    
    public boolean isCredentialsOk() {
        return mCredentials.getBoolean(Constants.PREFS_KEY_IS_VALID, false);
    }
    
    public void saveGeneralInfo(String name, String surname) {
        SharedPreferences.Editor editor = mGeneral.edit();
        editor.putString(Constants.PREFS_KEY_NAME, name);
        editor.putString(Constants.PREFS_KEY_SURNAME, surname);
        editor.commit();
    }
    
    public void savePicsPath(String picsPath) {
        SharedPreferences.Editor editor = mGeneral.edit();
        editor.putString(Constants.PREFS_KEY_PICS_PATH, picsPath);
        editor.commit();
    }
    
    private interface Constants {

        public static final String PREFS_CREDENTIALS = "credentials";

        public static final String PREFS_KEY_IS_VALID = "isValid";

        public static final String PREFS_KEY_USERNAME = "username";

        public static final String PREFS_KEY_PASSWORD = "password";

        public static final String PREFS_KEY_MD5_PASSWORD = "md5Password";
        
        public static final String PREFS_GENERAL = "general";
        
        public static final String PREFS_KEY_PICS_PATH = "picsPath";
        
        public static final String PREFS_KEY_NAME = "name";
        
        public static final String PREFS_KEY_SURNAME = "surname";
        
        public static final String PIC_NAME_FORMAT = "%s_%s_M%d_%d.jpg";
    }
}
